package com.ykdl.member.kid.adapters;

import java.util.Date;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.adapters.CouponFragmentAdapter.onSelectListener;
import com.ykdl.member.kid.beans.TicketBean;
import com.ykdl.member.kid.marketcard.CouponinfoWebView;
import com.ykdl.member.views.CircularImage;
/********************************************************************************

** 作者： jiafeiyu
** 创始时间：2015-4-27
** 描述：
**    我的优惠券之adapter

*********************************************************************************/
public class MyCouponAdapter extends BaseAdapter{

	private List<TicketBean>  tickets;
	private BitmapUtils bitmapUtil;
	private Context mContext ;
	private LayoutInflater inflater ;
	private int flag = -1;//用于区分是来之哪个 fragment   未使用 0  已使用 1 已过期 2
	public MyCouponAdapter(Context context,int flag){
		mContext = context;
		bitmapUtil = new BitmapUtils(context);
		bitmapUtil.configDefaultLoadingImage(R.drawable.defult_head_icon);
		bitmapUtil.configDefaultLoadFailedImage(R.drawable.defult_head_icon);
		inflater = LayoutInflater.from(context);
		this.flag = flag;
	}
	
	public void setData(List<TicketBean>  tickets){
		this.tickets = tickets;
	}
	
	
	
	@Override
	public int getCount() {
		if (tickets!=null&&tickets.size()>0) {
			return tickets.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if (convertView == null) {
			holder=new ViewHolder();
			convertView = inflater.inflate(R.layout.item_mycoupon_fragment_adapter, null);
			holder.ll_item_mycoupon_whole_back =(RelativeLayout) convertView.findViewById(R.id.ll_item_mycoupon_whole_back);
			holder.iv_mycoupon_fragment_back_logo =(CircularImage) convertView.findViewById(R.id.iv_mycoupon_fragment_back_logo);
			holder.iv_mycoupon_fragment_back_vip =(ImageView) convertView.findViewById(R.id.iv_mycoupon_fragment_back_vip);
			holder.tv_mycoupon_fragment_address =(TextView) convertView.findViewById(R.id.tv_mycoupon_fragment_address);
			holder.tv_mycoupon_fragment_ticket_number =(TextView) convertView.findViewById(R.id.tv_mycoupon_fragment_ticket_number);
			holder.tv_mycoupon_item_xilie =(TextView) convertView.findViewById(R.id.tv_mycoupon_item_xilie);
			holder.tv_mycoupon_out_of_date =(TextView) convertView.findViewById(R.id.tv_mycoupon_out_of_date);
			
			holder.ll_item_mycoupon_diyong =(LinearLayout) convertView.findViewById(R.id.ll_item_mycoupon_diyong);
			holder.tv_mycoupon_fragment_diyong_number =(TextView) convertView.findViewById(R.id.tv_mycoupon_fragment_diyong_number);
			
			holder.ll_item_mycoupon_manzeng =(LinearLayout) convertView.findViewById(R.id.ll_item_mycoupon_manzeng);
			holder.tv_mycoupon_fragment_manzeng_content =(TextView) convertView.findViewById(R.id.tv_mycoupon_fragment_manzeng_content);
			
			holder.ll_item_mycoupon_zhijiang =(LinearLayout) convertView.findViewById(R.id.ll_item_mycoupon_zhijiang);
			holder.tv_mycoupon_fragment_zhijiang_number =(TextView) convertView.findViewById(R.id.tv_mycoupon_fragment_zhijiang_number);
			
			holder.ll_item_mycoupon_manjian =(LinearLayout) convertView.findViewById(R.id.ll_item_mycoupon_manjian);
			holder.tv_mycoupon_fragment_manjian_content =(TextView) convertView.findViewById(R.id.tv_mycoupon_fragment_manjian_content);
			holder.tv_mycoupon_fragment__number =(TextView) convertView.findViewById(R.id.tv_mycoupon_fragment__number);
			
			
			
			
			
			convertView.setTag(holder);
		}else{
			holder=(ViewHolder) convertView.getTag();
		}
		
		initViews(holder,position);
		
		return convertView;
	}
	
	
	//优惠劵类型:1  # 抵用  2  # 满减  3  # 买赠  4  # 直降
	private void initViews(ViewHolder holder, int position) {
		if (tickets==null||tickets.size()==0) {
			return ;
		}
		TicketBean info = tickets.get(position);
		switch (info.getCoupon_type()) {
		case 1:
			holder.ll_item_mycoupon_whole_back.setBackgroundResource(R.drawable.coupon_green_back);
			holder.ll_item_mycoupon_diyong.setVisibility(View.VISIBLE);
			holder.ll_item_mycoupon_manzeng.setVisibility(View.GONE);
			holder.ll_item_mycoupon_zhijiang.setVisibility(View.GONE);
			holder.ll_item_mycoupon_manjian.setVisibility(View.GONE);
			holder.tv_mycoupon_fragment_diyong_number.setText(info.getCoupon_num_1());
			break;
		case 2:
			holder.ll_item_mycoupon_whole_back.setBackgroundResource(R.drawable.coupon_red_back);	
			holder.ll_item_mycoupon_diyong.setVisibility(View.GONE);
			holder.ll_item_mycoupon_manzeng.setVisibility(View.GONE);
			holder.ll_item_mycoupon_zhijiang.setVisibility(View.GONE);
			holder.ll_item_mycoupon_manjian.setVisibility(View.VISIBLE);
			holder.tv_mycoupon_fragment_manjian_content.setText("满"+info.getCoupon_num_1()+"减");
			holder.tv_mycoupon_fragment__number.setText(info.getCoupon_num_2());
			break;
		case 3:
			holder.ll_item_mycoupon_whole_back.setBackgroundResource(R.drawable.coupon_yellow_back);
			holder.ll_item_mycoupon_diyong.setVisibility(View.GONE);
			holder.ll_item_mycoupon_manzeng.setVisibility(View.GONE);
			holder.ll_item_mycoupon_zhijiang.setVisibility(View.GONE);
			holder.ll_item_mycoupon_manjian.setVisibility(View.VISIBLE);
			holder.tv_mycoupon_fragment_manjian_content.setText("买"+info.getCoupon_num_1()+"赠");
			holder.tv_mycoupon_fragment__number.setText(info.getCoupon_num_2());
//			holder.tv_mycoupon_fragment_manzeng_content.setText("买"+info.getCoupon_num_1()+"赠"+info.getCoupon_num_2());
			
			break;
		case 4:
			holder.ll_item_mycoupon_whole_back.setBackgroundResource(R.drawable.coupon_blue_back);
			holder.ll_item_mycoupon_diyong.setVisibility(View.GONE);
			holder.ll_item_mycoupon_manzeng.setVisibility(View.GONE);
			holder.ll_item_mycoupon_zhijiang.setVisibility(View.VISIBLE);
			holder.ll_item_mycoupon_manjian.setVisibility(View.GONE);
			holder.tv_mycoupon_fragment_zhijiang_number.setText(info.getCoupon_num_1());
			break;

		default:
			break;
		}
		
		 if (info.isMembers_only()) {
			 
			 holder.iv_mycoupon_fragment_back_vip.setVisibility(View.VISIBLE); 
			 if (flag==2) {
				 holder.iv_mycoupon_fragment_back_vip.setBackgroundResource(R.drawable.mycoupon_vip_gray);
			}
		 }else {
			 holder.iv_mycoupon_fragment_back_vip.setVisibility(View.GONE);
		 }
		//劵号隐藏
//		if (info.isIs_ticket_code()) {
//			holder.tv_mycoupon_fragment_ticket_number.setVisibility(View.VISIBLE);
//			holder.tv_mycoupon_fragment_ticket_number.setText("券号:"+info.getTicket_id());
//		}else {
//			holder.tv_mycoupon_fragment_ticket_number.setVisibility(View.INVISIBLE);
//		}
		
		String url = info.getLogo().getFile_url();
		if (!TextUtils.isEmpty(url)) {
			 bitmapUtil.display(holder.iv_mycoupon_fragment_back_logo, url);
		}
		
		holder.tv_mycoupon_fragment_address.setText(info.getStore().getName());
		//TODO 没有系列的数据字段，后期添加
		holder.tv_mycoupon_item_xilie.setText(info.getDiscount_to());
		//holder
		switch (flag) {
		case 0:
			if (getOutofDate(info.getValid_end_time())==0) {
				holder.tv_mycoupon_out_of_date.setText("明天到期");
			}else {
				holder.tv_mycoupon_out_of_date.setText("还剩"+getOutofDate(info.getValid_end_time())+"天过期");
			}
			
			break;
		case 1:
			holder.tv_mycoupon_out_of_date.setText("已使用");		
			break;
		case 2:
			holder.tv_mycoupon_out_of_date.setText("已过期");	
			holder.ll_item_mycoupon_whole_back.setBackgroundResource(R.drawable.mycoupon_back_gray);
			
			break;

		default:
			break;
		}
		holder.ll_item_mycoupon_whole_back.setTag(info);
		 holder.ll_item_mycoupon_whole_back.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO 进行查询优惠券详情的跳转
					TicketBean info = (TicketBean) v.getTag();
					Intent intent = new Intent(mContext, CouponinfoWebView.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					intent.putExtra(KidAction.COUPON_ID, info.getTicket_id());
					intent.putExtra(KidAction.URL_TYPE, 0);
					mContext.startActivity(intent);
				}
			});		
	}
	class ViewHolder {
		RelativeLayout ll_item_mycoupon_whole_back;
		CircularImage iv_mycoupon_fragment_back_logo;
		ImageView iv_mycoupon_fragment_back_vip;
		TextView tv_mycoupon_fragment_address;
		TextView tv_mycoupon_fragment_ticket_number;
		TextView tv_mycoupon_item_xilie;
		TextView tv_mycoupon_out_of_date;
		//抵用
		LinearLayout ll_item_mycoupon_diyong;
		TextView tv_mycoupon_fragment_diyong_number;
		//买赠
		LinearLayout ll_item_mycoupon_manzeng;
		TextView tv_mycoupon_fragment_manzeng_content;
		//直降
		LinearLayout ll_item_mycoupon_zhijiang;
		TextView tv_mycoupon_fragment_zhijiang_number;
		//满减
		LinearLayout ll_item_mycoupon_manjian;
		TextView tv_mycoupon_fragment_manjian_content;
		TextView tv_mycoupon_fragment__number;
	}
	
	
	
	private int getOutofDate(String date){
		if (!TextUtils.isEmpty(date)) {
			
			Date mdate = new Date(Math.round(Double.valueOf(date).doubleValue()*1000));
			
			long old = mdate.getTime();
			long now = System.currentTimeMillis();
			
			long num = old - now;
			
			
			long nums = num/1000;
			long numss = nums/3600;
			long numa = numss/24;
			
			int day = (int) numa;
			return day;
		}
		return 0;
	}
}
